package com.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import java.time.chrono.MinguoChronology;

@Api("邮件发送通用接口")
@RestController
public class EmailController {

    @Autowired
    private JavaMailSender jms;

    @Value("${spring.mail.username}")
    private String userName;

    @RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
    public String sendEmail() {
        System.out.println("进入发送邮件普通方法");
        String str = null;
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(userName);
            message.setTo("3448741711@qq.com");
            message.setSubject("发送一封简单邮件");
            message.setText("欢迎假期来学习大数据应用技术");
            jms.send(message);
            str = "邮件发送成功";
        } catch (Exception e) {
            System.out.println("发送邮件失败！原因是：" + e.getMessage());
            str = "发送失败";
        }

        return str;
    }

    @RequestMapping(value = "/sendHtmlEmail", method = RequestMethod.POST)
    public String sendHtmlEmail() {
        System.out.println("进入发送html内容的邮件方法");
        String str = "";
        try {
            MimeMessage message = null;
            message = jms.createMimeMessage();

            MimeMessageHelper html = new MimeMessageHelper(message, true);
            html.setFrom(userName);
            html.setTo("3448741711@qq.com");
            html.setSubject("html的一封邮件");
            StringBuffer sb = new StringBuffer("<p style='color: #42b98e'>使用大数据分析内容，通过html格式将内容发送到网页上。</p>");

            html.setText(sb.toString(), true);

            jms.send(message);

            str = "html邮件发送成功";

        } catch (Exception e) {
            System.out.println("发送html邮件失败！原因是：" + e.getMessage());
            str = "发送失败";
        }

        return str;
    }

    @RequestMapping(value = "/sendFuJian", method = RequestMethod.POST)
    public String sendFuJian() {
        System.out.println("进入发送附件的邮件方法");
        String str = "";
        try {
            MimeMessage message = null;
            MimeMessageHelper html = new MimeMessageHelper(message, true, "UTF-8");

            html.setFrom(userName);
            html.setTo("3448741711@qq.com");
            html.setSubject("发送一个带附件的邮件");
            html.setText("详情请下载附件:");
            FileSystemResource file = new FileSystemResource("src\\main\\resources\\static.file\\测试结果.docx");
            html.addAttachment("测试结果.docx", file);

            jms.send(message);
            str = "发送带附件的邮件成功";
        } catch (Exception e) {
            System.out.println("发送带附件的邮件失败！原因是：" + e.getMessage());
            str = "发送失败";
        }

        return str;
    }

    @RequestMapping(value = "/sendImgEmail", method = RequestMethod.POST)
    public String sendImgEmail() {
        System.out.println("进入发送带有图片的邮件方法");
        String str = "";
        try {
            MimeMessage message = null;
            message = jms.createMimeMessage();
            MimeMessageHelper html = new MimeMessageHelper(message, true, "UTF-8");

            html.setFrom(userName);
            html.setTo("3448741711@qq.com");
            html.setSubject("发送带有图片的网页");
            String txt = "<html><body>我来也：<img src='cis:img'/></body></html>";
            html.setText(txt);
            FileSystemResource fileImg = new FileSystemResource("src\\main\\resources\\img\\sunshine.png");
            html.addAttachment("sunshine.png", fileImg);

            jms.send(message);
            str = "发送带图片的邮件成功";
        } catch (Exception e) {
            System.out.println("发送带附件的邮件失败！原因是：" + e.getMessage());
            str = "发送失败";
        }

        return str;
    }
}
